<?php
namespace Admin\Model;

use Think\Model\RelationModel;

class DepartModel extends RelationModel
{
    protected $token = 'ofanap1448334221';

    protected $connection = array(
        'db_type'       =>  'mysql',
        'db_host'       =>  '114.55.30.131',
        'db_port'       =>  '3306',
        'db_name'       =>  'wxsmart',
        'db_user'       =>  'zhkj_crm',
        'db_pwd'        =>  'AdkdCGP7fHcjht5c',
        'db_charset'    =>  'utf8',
        'db_debug'      =>  true,
    );
    protected $tablePrefix = '';
    protected $tableName = 'tp_department';

    protected $_auto = array(
    );

    public function updateDept($community=false){
        if(!$community) {
            $data['name'] = I('name');
            $data['parentid'] = I('parentid');
            $data['order'] = I('sort');
            $data['id'] = I('id');
        }else{
            $data['name'] = $community['name'];
            $data['parentid'] = $community['parentid'];
            $data['order'] = $community['sort'];
            $data['id'] = $community['id'];
        }
        $ent = new \ZL\Wechat\EnterpriseHelper($this->token);
        $res = $ent->updateDept($data);
        $res= json_decode($res,true);
        if($community) {
            $data['community_id'] = $community['community_id'];
//            myDump($data);
            $this->data($data)->save();
        }
        if($res['errcode']==0) {
            $data['sort'] = I('sort',0);
            unset($data['order']);
            if($community)
                $data['community_id'] = $community['community_id'];
            $this->data($data)->save();
            return true;
        }
    }
    public function addDept($community=false){
        $ent = new \ZL\Wechat\EnterpriseHelper($this->token);

        if(!$community) {
            $data['name'] = I('name');
            $data['parentid'] = I('parentid');
            $data['order'] = I('sort');
            $res = $ent->createDept($data);
        }else{
            $now_data = $this->chckRepair($community);
            //已经创建   去更新
            if($now_data){
                $now_data['name'] = $community['name'];
                $now_data['community_id'] = $community['id'];
//                myDump($now_data);
                $this->updateDept($now_data);
                return true;
            }else {
                //没有创建 添加
                $data['name'] = $community['name'];
                $data['parentid'] = 14;
                $res = $ent->createDept($data);
//                echo '添加-'.$community['name'];
//                myDump($res);
            }
        }

//        $data = json_encode($data);
//        var_dump($data);
//        var_dump($res);die;
        $res= json_decode($res,true);
        if($res['errcode']==0) {
            $data['sort'] = I('sort',0);
            unset($data['order']);
            $data['id'] = $res['id'];
            if($community)
                $data['community_id'] = $community['id'];
            $this->data($data)->add();
            return true;
        }
    }

    public function chckRepair($community){
        $name_data = $this->where(array('name'=>$community['name']))->find();
        $community_data = $this->where(array('community_id'=>$community['id']))->find();
        $data = $community_data?$community_data:$name_data;
        return $data;
    }
    public function getRepair($depId=false){
        $people = array();
        $all = array();
        if(!$depId) {
            $data = $this->where(array('parentid' => 14))->select();
            foreach ($data as $v) {
                $people[$v['name']] = D('Admin/Employee')->where(array('department' => array('like','%'.$v['id'].'%')))->select();
            }

        }else{
            $depData = $this->find($depId);
            $data52 = $this->find(52);
            $data53 = $this->find(53);
            $depData = $this->find($depId);
            $people[$depData['name']] = D('Admin/Employee')->where("department like '%$depId%'")->select();
            $people[$data52['name']] = D('Admin/Employee')->where("department = 52")->select();
            $people[$data53['name']] = D('Admin/Employee')->where("department = 53")->select();
        }
        foreach ($people as $k => &$v) {
            foreach ($v as &$one) {
                $all[$one['id']] = $one;
                if ($all[$one['id']]['repair_address'])
                    $all[$one['id']]['repair_address'] .= ',' . $k;
                else
                    $all[$one['id']]['repair_address'] = $k;
                $all[$one['id']]['star_level'] =D('Admin/Employee')->getLevel($one['id']);
            }
        }
//        myDump($all);
//        myDump($people);die;
        return $all;
    }

    public function getDepName(&$people,$type=1){
        if($type==1) {
            $people['department_arr'] = explode(',', $people['department']);
            foreach ($people['department_arr'] as $v) {
                $data[] = $this->where(['id' => $v])->find();
            }
            $dep_name = '';
            foreach ($data as $dep) {
                if ($dep_name)
                    $dep_name .= ',' . $dep['name'];
                else
                    $dep_name = $dep['name'];
            }
            $people['department_name'] = $dep_name;
            $this->getPositionName($people);
        }else {
            foreach ($people as &$one) {
                $one['department_arr'] = explode(',', $one['department']);
//                myDump($one['department']);
                $data = array();
                foreach ($one['department_arr'] as $v) {
                    $data[] = $this->where(['id' => $v])->find();
                }
                $dep_name = '';
                foreach ($data as $dep) {
                    if ($dep_name)
                        $dep_name .= ',' . $dep['name'];
                    else
                        $dep_name = $dep['name'];
                }
                $one['department_name'] = $dep_name;
                $this->getPositionName($one);
            }
        }
    }

    public function getPositionName(&$people){
        switch ($people['position']){
            case 'repair':
                $people['position_title'] = '维修工';
                break;
            case 'manager':
                $people['position_title'] = '项目经理';
                break;
            default:
                $people['position_title'] = '维修工';
        }
    }

    public function _before_write(&$data)
    {
        parent::_before_write($data); // TODO: Change the autogenerated stub
    }

    public function _after_insert($data, $options)
    {
        parent::_after_insert($data, $options); // TODO: Change the autogenerated stub
    }
}
